package util

import (
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/encoding/unicode"
	"strings"
)

// FirstUpper 字符串首字母大写

func FirstUpper(s string) string {

	if s == "" {

		return ""

	}

	return strings.ToUpper(s[:1]) + s[1:]

}

// FirstLower 字符串首字母小写

func FirstLower(s string) string {

	if s == "" {

		return ""

	}

	return strings.ToLower(s[:1]) + s[1:]

}

func Forward(s string) string {
	if s == "" {
		return ""
	}
	if strings.Contains(s, ".") {
		return strings.Split(s, ".")[0]
	} else {
		return ""
	}
}

func Backward(s string) string {
	if s == "" {
		return ""
	}
	if strings.Contains(s, ".") {
		return strings.Split(s, ".")[1]
	} else {
		return s
	}
}

func FromUTF16(d []byte)(string, error){
	decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()
	bs2, err := decoder.Bytes(d[:])
	if err != nil{
		return "", err
	}
	return string(bs2), nil
}

func ToUTF16(e []byte)([]byte, error) {
	encoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
	bs2, err := encoder.Bytes(e)
	if err != nil{
		return nil, err
	}
	return bs2, nil
}

func FromANSI(d []byte)(string, error){
	decoder := simplifiedchinese.GBK.NewDecoder()
	bs2, err := decoder.Bytes(d[:])
	if err != nil{
		return "", err
	}
	return string(bs2), nil
}

func ToANSI(e []byte)([]byte, error) {
	encoder := simplifiedchinese.GBK.NewEncoder()
	bs2, err := encoder.Bytes(e)
	if err != nil{
		return nil, err
	}
	return bs2, nil
}

